package lablayer.view;

import lablayer.model.Componentable;
import javafx.scene.CustomNode;
import lablayer.model.LabData;
import java.lang.UnsupportedOperationException;
import java.util.UUID;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author darkness
 */
public abstract class ViewComponent extends CustomNode, ViewComponentInterface {

    protected var component: Componentable;

    protected var xml: String;

	/*
	*	unique identificator of instance of ViewComponent for XML representation
	*/
	protected var viewComponentId: String;

	init {
		this.viewComponentId = UUID.randomUUID().toString();
	}

    override public function getComponent(): Componentable {
        return this.component;
    }

    public function saveToXML(): String {
		var xml: String = "";
		xml += "	<ViewComponent>\r\n";
		xml += "		<Type>{this.component.getComponentId()}</Type>\r\n";
		xml += "		<TranslateX>{this.translateX}</TranslateX>\r\n";
		xml += "		<TranslateY>{this.translateY}</TranslateY>\r\n";
		xml += "		<ViewComponentId>{this.getViewComponentId()}</ViewComponentId>\r\n";
		
		if (this.component != null) {
			xml += "		<Component>\r\n";
			var labData: LabData;
			try {
				labData = this.component.getInitData();
				xml += "			<InitData>{labData.toString()}</InitData>\r\n";
			} catch (e: UnsupportedOperationException) {}
			try {
				labData = this.component.getData();
				xml += "			<Data>{labData.toString()}</Data>\r\n";
			} catch (e: UnsupportedOperationException) {}
			try {
				labData = this.component.getResult();
				xml += "			<Result>{labData.toString()}</Result>\r\n";
			} catch (e: UnsupportedOperationException) {}
			xml += "		</Component>\r\n"; //<Component ... />
			xml += "	</ViewComponent>\r\n";
		} else {
			xml += "	</ViewComponent>\r\n";
		}		
		return xml;
	}

	public function loadFromXML(vcNode: Node): Void {
		if (vcNode != null) {
			if (vcNode.getNodeType() == Node.ELEMENT_NODE) {
				var vcElement: Element = vcNode as Element;

				// get translateX
				var txList: NodeList = vcElement.getElementsByTagName("TranslateX");
				var txElem: Element = txList.item(0) as Element;
				var translateX: String = txElem.getChildNodes().item(0).getNodeValue();
				this.translateX = Float.parseFloat(translateX);

				// get translateY
				var tyList: NodeList = vcElement.getElementsByTagName("TranslateY");
				var tyElem: Element = tyList.item(0) as Element;
				var translateY: String = tyElem.getChildNodes().item(0).getNodeValue();
				this.translateY = Float.parseFloat(translateY);

				// get viewComponentId
				var viewComponentIdList: NodeList = vcElement.getElementsByTagName("ViewComponentId");
				var viewComponentIdElem: Element = viewComponentIdList.item(0) as Element;
				var viewComponentId: String = viewComponentIdElem.getChildNodes().item(0).getNodeValue();
				this.viewComponentId = viewComponentId;

				// digging Component...
				var componentList: NodeList = vcElement.getElementsByTagName("Component");
				// get Data
				var componentElem: Element = componentList.item(0) as Element;
				var dataList: NodeList = componentElem.getElementsByTagName("Data");
				var dataElem: Element = dataList.item(0) as Element;
				var data: String = dataElem.getChildNodes().item(0).getNodeValue();
				this.component.setData(new LabData(LabData.parseString(data)));
			}
		}
	}


    override public abstract function update(): Void;

	public function getViewComponentId(): String {
		return this.viewComponentId;
	}
}